<template>
  <div class='search'>
    <f-form ref='form' :action='api.payment' :form-key='this.formItem.paymentId' :file-ids='fileIds' handlerName='refundHkPaymentSaveHandler'>
      <f-form-item label='收款单位' name='payeeName' :value='formItem.payeeName' />
      <f-form-item label='原支付账户' disabled name='oldBankNo' :value='formItem.bankNo' />
      <f-form-item label='收款账户' placeholder='请选择付款账户' name='bankName' :readOnly='true' :required='true' :value.sync='formItem.bankName' :trigger-change='true' v-if='formItem.customId'>
        <a-tooltip slot='addonBefore' placement='top' title='点击图标选择'>
          <a-icon type='money-collect' style='cursor:pointer' @click='openCustomBankModal' v-if='formItem.amountType=="OrderAmount"' />
          <a-icon type='money-collect' style='cursor:pointer' @click='openCustomBankModal' v-if='formItem.amountType=="RefundHk"' />
          <a-icon type='money-collect' style='cursor:pointer' @click='openSaleManBankModal' v-if='formItem.amountType=="CustomBond"' />
        </a-tooltip>
      </f-form-item>
      <!--customId不存在时可自定义填写收款银行-->
      <f-form-item label='收款银行' name='bankName' :required='true' :value='formItem.bankName' v-if='!formItem.customId'/>
      <f-form-item label='收款账户' name='bankNo' :required='true' :value='formItem.bankNo' v-if='!formItem.customId'/>
      <f-form-item label='申请部门' name='applyDeptName' :required='true' :value='formItem.applyDeptName' />
      <f-form-item label='付款事项' name='title' :required='true' :value='formItem.title' />
      <f-form-item label='退款金额' required dataType='number' name='refundAmount' :max='formItem.amount' type='number' :value='formItem.amount' />
      <f-form-item label='附件' type='upload' name='paymentImage' :value='formItem.paymentImage' :file-max='3' :busId='this.formItem.paymentId' busType='BusPayment' :realRemove='true' @uploadSuccess='uploadSuccess' />
      <f-form-item label='备注' type='textarea' name='remark' :value='formItem.remark' />
    </f-form>
    <select-custom-bank-modal ref='customBankModal' @onSelectChange='onSelectCustomBankChange'></select-custom-bank-modal>
    <select-sale-man-bank-modal ref='saleManBankModal' @onSelectChange='onSelectSaleManBankChange'></select-sale-man-bank-modal>
  </div>
</template>
<script>
import api from "../../js/api"
import { commonMixins } from "@/core/mixins/commonMixins"
import SelectCustomBankModal from "@/views/module/haiying/components/select_custom_bank_modal"
import SelectSaleManBankModal from "@/views/module/haiying/components/select_saleman_bank_modal"

export default {
  name: "amountForm",
  props: {},
  mixins: [commonMixins],
  data() {
    return {
      api: Object.assign(api, this.api),
      selectIds: null,
      paymentId: null,
      fileIds: [],//上传文件ID
      removeIds: [],//删除文件
      formItem: this.getFormItem()
    }
  },
  methods: {
    getFormItem() {
      return {
        depositId: "",
        refundAmount: 0,
        payAmount: 0,
        refundDate: null,
        remark: null
      }
    },

    //表单赋值
    setData(data) {
      this.$refs.form.resetFields()
      this.formItem = this.getFormItem()
      this.isAdd = data.isAdd
      if (data && data.selectIds && this.isAdd) {
        this.selectIds = data.selectIds
        this.getRefundHkInfo()
      }
      if (data.paymentId) {
        this.http.get(this, {
          url: this.api.payment.get,
          params: {
            paymentId: data.paymentId,
            handlerName: "hyRefundPaymentGetHandler",
            fileGroup: 1
          },
          success: (me, data) => {
            this.$nextTick(() => {
              this.formItem = data
              this.selectIds = data.selectIds
            })
          }
        })
      }
    },
    /**
     * 获取退货款金信息
     */
    getRefundHkInfo() {
      this.http.get(this, {
        url: this.api.payment.getRefundHkInfo,
        params: {
          selectIds: this.selectIds
        },
        success: (me, data) => {
          this.$nextTick(() => {
            this.formItem = data
            if (data.payeeName) {
              this.formItem.payeeName = data.payeeName
              this.formItem.amount = data.amount
              this.formItem.customId = data.customId
              this.$refs.form.getForm().setFieldsValue({ "payeeName": data.payeeName })
              this.$refs.form.getForm().setFieldsValue({ "amount": data.amount })
            }
            this.$forceUpdate()
          })
        }
      })
    },
    /**
     * 付款银行选择回调
     */
    onSelectCustomBankChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "bankName": selectedRows[0].bankName })
      this.formItem.bankName = selectedRows[0].bankName
      this.formItem.bankId = selectedRows[0].customBankId
      this.$forceUpdate()
    },
    /**
     * 收款账户选择回调
     */
    onSelectSaleManBankChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "bankName": selectedRows[0].bankName })
      this.formItem.bankId = selectedRowKeys[0]
      this.formItem.bankName = selectedRows[0].bankName
      this.$forceUpdate()
    },
    /**
     * 文件上传回调
     */
    uploadSuccess(fileUrl, files, fileIds, removeIds) {
      this.fileIds = fileIds
      this.removeIds.push(...removeIds)
    },
    /**
     * 选择收款账户
     */
    openSaleManBankModal() {
      this.$refs["saleManBankModal"].open({
        saleManId: this.formItem.saleManId
      })
    },
    //提交表单
    handleSubmit(callback) {
      this.$refs.form.submit({
        bankId: this.formItem.bankId,
        selectIds: this.selectIds,
        amountType: this.formItem.amountType
      }, callback)
    }
  },
  created() {
  },
  components: { SelectSaleManBankModal, SelectCustomBankModal }
}
</script>

<style scoped lang='less' rel='stylesheet/less'>

</style>
